package com.fps.web.dao.impl;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.fpswork.core.exception.exception.BusinessException;
import com.fpswork.core.model.PageEntity;
import com.fpswork.datamodel.basedao.BaseDao;
import com.fpswork.util.BeanUtils;
import com.fpswork.util.StringUtil;
import com.fps.web.dao.BasememoryDao;
import com.fps.web.model.Basememory;
@Repository 
public class BasememoryDaoImpl extends BaseDao implements BasememoryDao{
    
	public PageEntity queryBasememoryList(PageEntity pageEntity)  throws BusinessException{
		String sql="select * from basememory where 1=1 ";
		if(pageEntity.getSearch()!=null&&StringUtil.isNotEmpty(pageEntity.getSearch().get("type"))){
			sql+=" and basememory.type='"+pageEntity.getSearch().get("type")+"' ";
		}
		if(pageEntity.getSearch()!=null&&StringUtil.isNotEmpty(pageEntity.getSearch().get("createuserid"))){
			sql+=" and basememory.createuserid='"+pageEntity.getSearch().get("createuserid")+"' ";
		}
		pageEntity.setSql(sql);
		return  this.queryForPageEntity(pageEntity);
	}
	public Basememory queryBasememoryByID(String id)  throws BusinessException{
		return  this.get(Basememory.class, id);
	}
	public Basememory addBasememory(Basememory basememory)  throws BusinessException{
		//校验是否为空
		if(basememory==null){
			throw new BusinessException("传入数据为空！");
		}
		if(StringUtil.isEmpty(basememory.getId())){
			this.save(basememory);
		}else{
			throw new BusinessException("已存在数据！");
		}
		return basememory;
	}
	public Basememory updateBasememory(Basememory basememory)  throws BusinessException{
		basememory.setContent(StringUtil.turn(basememory.getContent()));
		Basememory basememory1= this.get(Basememory.class, basememory.getId());
		if(basememory1==null||StringUtil.isEmpty(basememory1.getId())){
	 		throw new BusinessException("没有找到数据！");
		}else{
			List<String> copyPropertiesList = new ArrayList<String>();
			copyPropertiesList.add("content");
			copyPropertiesList.add("type");
			BeanUtils.copyPartProperties(basememory, basememory1,copyPropertiesList);
			this.update(basememory1);
		}
		return basememory1;
	}
	public Boolean deleteBasememory(Basememory basememory)  throws BusinessException{
		this.delete(this.get(Basememory.class, basememory.getId()));
		
		return true;
	}
}
